package com.solver.demo.rscoket.v1;

import io.rsocket.transport.netty.client.TcpClientTransport;
import org.springframework.messaging.rsocket.RSocketRequester;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Flux;

@Component
public class V1RSocketClient {

    private final RSocketRequester.Builder requesterBuilder;

    public V1RSocketClient(RSocketRequester.Builder requesterBuilder) {
        this.requesterBuilder = requesterBuilder;
    }

    public void test() {
        RSocketRequester requester = requesterBuilder
                .transport(TcpClientTransport.create("localhost", 8081));

        // Request-Response
//        requester.route("request-response")
//                .data("Hello")
//                .retrieveMono(String.class)
//                .subscribe(response -> System.out.println("Request-Response: " + response));
//
//        // Request-Stream
//        requester.route("request-stream")
//                .data("Hello")
//                .retrieveFlux(String.class)
//                .subscribe(response -> System.out.println("Request-Stream: " + response));
//
//        // Fire-and-Forget
//        requester.route("fire-and-forget")
//                .data("Hello")
//                .send()
//                .subscribe();

        // Channel
        String groupId = "group-1"; // 为每组请求生成一个唯一的ID
        Flux<Message> requests = Flux.just(
                new Message(groupId, "Message 1"),
                new Message(groupId, "Message 2"),
                new Message(groupId, "Message 3")
        );

        requester.route("channel")
                .data(requests)
                .retrieveFlux(String.class)
                .subscribe(response -> System.out.println("Channel: " + response));
    }
}
